package com.boc.langxiang.ui.adapter.index;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.boc.langxiang.R;
import com.boc.langxiang.entity.response.IndexAdmEntity;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.ui.activity.adm.ArtistIndexActivity;
import com.boc.langxiang.ui.activity.common.RegistAndLoginActivity;
import com.boc.langxiang.ui.view.SongTextView;
import com.boc.langxiang.util.L;
import com.boc.langxiang.util.StringUtil;

import java.util.List;

public class IndexCoverPagerAdapter extends PagerAdapter {

    private Context mContext;
    private List<IndexAdmEntity> mDatas;

    public IndexCoverPagerAdapter(Context context, List<IndexAdmEntity> datas) {
        mContext = context;
        mDatas = datas;
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_index_cover, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.iamge);
        SongTextView name = (SongTextView) view.findViewById(R.id.name);
        SongTextView job = (SongTextView) view.findViewById(R.id.job);
        SongTextView artCount = (SongTextView) view.findViewById(R.id.art_count);
        SongTextView hotCount = (SongTextView) view.findViewById(R.id.hot_count);
        View clickGp = view.findViewById(R.id.clickgp);
        IndexAdmEntity indexAdmEntity = mDatas.get(position);
        clickGp.setTag(indexAdmEntity);
        if (indexAdmEntity != null) {
            String photourl = indexAdmEntity.getPhotourl();
            PicassoImageLoader.getInstance().displayUrlImage(mContext,photourl, imageView);
        } else {
            PicassoImageLoader.getInstance().displayUrlImage(mContext,null, imageView);

        }
        name.setText(StringUtil.getValue(indexAdmEntity.getNickname()));
        job.setText(StringUtil.getValue(indexAdmEntity.getZhiwu()));
        artCount.setText(StringUtil.getValue("作品 " + indexAdmEntity.getOwn_product_count()));
        hotCount.setText(StringUtil.getValue("热度 " + indexAdmEntity.getOwn_click_count()));

        container.addView(view);

        clickGp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                String token = UserInfoManager.getInstance().getToken();
//                if (StringUtil.isEmpty(token)) {
//                    Intent intent = new Intent(mContext, RegistAndLoginActivity.class);
//                    mContext.startActivity(intent);
//                    return;
//                }
                IndexAdmEntity entity = (IndexAdmEntity) v.getTag();
                L.i(entity.getId());
                Intent intent = new Intent(mContext, ArtistIndexActivity.class);
                intent.putExtra("designerId", entity.getId());
                mContext.startActivity(intent);
            }
        });
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        if (mDatas == null) {
            return 0;
        }
        return mDatas.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view == object);
    }
}
